package top.psjj.news.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import top.psjj.news.po.TLink;
import top.psjj.news.utils.DataSourceUtils;

import java.util.List;

/**
 * @Auther:小陈的Java
 */
public class LinkDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource());
    public List<TLink> queryAll() throws Exception {
        String sql = "select id,link_name linkName,link_url linkUrl,order_num orderNum from t_link order by order_num asc";
        List<TLink> result = queryRunner.query(sql, new BeanListHandler<TLink>(TLink.class));
        return result;
    }
    public void add(TLink link) throws Exception{
        String sql = "insert into t_link(link_name,link_url,order_num) values(?,?,?)";
        queryRunner.update(sql,link.getLinkName(),link.getLinkUrl(),link.getOrderNum());
    }

    public TLink queryLink(int id) throws Exception{
        String sql = "select id,link_name linkName,link_url linkUrl,order_num orderNum from t_link  where id = ?";
        TLink link = queryRunner.query(sql,new BeanHandler<TLink>(TLink.class),id);
        return link;
    }

    public void update(TLink link) throws Exception{
        String sql = "update t_link set link_name=?,link_url=?,order_num=? where id=? ";
        queryRunner.update(sql,link.getLinkName(),link.getLinkUrl(),link.getOrderNum(),link.getId());
    }

    public void del(int id) throws Exception{
        String sql = "delete from t_link where id = ?";
        queryRunner.update(sql,id);
    }
}
